#include "EWAUITrackBody.hpp"

#include "EWAPrjTrackUI.hpp"
#include "EWAPrjFragmentFragmentResourse.hpp"

namespace Eyer
{
    EWAUITrackBody::EWAUITrackBody(EyerSmartPtr<EWAUITimelineContext> _timelineCtx, EWAUILayoutParams _width, EWAUILayoutParams _height)
        : EWAUIWidget(_width, _height)
        , timelineCtx(_timelineCtx)
    {
        SetWidgetName("EWAUITrackBody");
        SetLayoutType(EWAUILayoutType::LinearLayoutVertical);

        timelineCtx->timeline.RegisterTrackListener(this);
    }

    EWAUITrackBody::~EWAUITrackBody()
    {
        timelineCtx->timeline.UnregisterTrackListener(this);
    }

    int EWAUITrackBody::OnFragmentUpdate(EWAPrjTrack * track)
    {
        EyerLog("Fragment Update\n");
        RemoveAllWidget();
        EWAPrjTrackType trackType = GetTrackType();
        int trackCount = timelineCtx->timeline.GetTrackCount(trackType);
        for(int i = 0; i < trackCount; i++) {
            EyerSmartPtr<EWAPrjTrackUI> track = timelineCtx->timeline.GetTrack(i, trackType);
            for (int j = 0; j < track->GetFragmentCount(); j++) {
                EyerSmartPtr<EWAPrjFragmentFragmentResourse> fragment = track->GetFragment(j);
                AddWidget(fragment);
            }
        }
        return 0;
    }

    EWAPrjTrackType EWAUITrackBody::GetTrackType()
    {
        return EWAPrjTrackType::VIDEO;
    }
}